new Vue({
    el: "#container",
    data() {
        return {
            // 查询数据
            initIndentId: null,
            tableSearch: {
                pageIndex: 0,
                limit: 50,
                total: 0,
                dateFormat: "yyyy-MM-dd HH:mm",
                startTime: null,
                endTime: null,
                site: "",
                conAuto: null,
                linkErp: null,
            },
            //  表数据
            tableDatas: [],
            tableLoading: false,
            //  是否手动生产
            conAutoOptions: [{
                value: 0,
                label: "ERP生产"
            }, {
                value: 1,
                label: "工控生产"
            }],
            //  是否已关联ERP
            linkErpOptions: [{
                value: 0,
                label: "未关联"
            }, {
                value: 1,
                label: "已经关联"
            }],
            //  关联表单
            addInvDialog: {
                show: false,
                loading: false,
            },
            //  关联操作-表单数据
            addInvForm: {
                controlInvId: null,
                indentPopover: false,
                render: {
                    indentId: null,
                    sendStere: null,
                    proStere: null,
                    proTime: null,
                    isAdd: "0",
                    dispatcher: null,
                    mixUser: null,
                    dispatcherName: null,
                    mixUserName: null,
                    carId: null,
                    driver: null,
                    calcTranStere: "0",
                    receiptType: null,
                    specialClearType: null,
                    roadType: null,
                    remark: null,
                    tranRemark: null,
                    pumpRemark: null,
                },
            },
            //  转料验证操作
            linkRules: {
                //  上传数据
                render: {
                    indentId: [
                        {required: true, message: '请选择任务单', trigger: 'blur'},
                    ],
                    isAdd: [
                        {required: true, message: '请选择是否累计', trigger: 'blur'},
                    ],
                    dispatcherName: [
                        {required: true, message: '请选择调度员', trigger: 'blur'},
                    ],
                    mixUserName: [
                        {required: true, message: '请选择拌台操作员', trigger: 'blur'},
                    ],
                    carId: [
                        {required: true, message: '请选择运输车辆', trigger: 'blur'},
                    ],
                    calcTranStere: [
                        {required: true, message: '请选择是否计算运输方量', trigger: 'blur'},
                    ],
                },
            },
            //  车辆列表
            carList: [],
            //  调度员列表
            dispatcherList: [],
            // 操作员列表
            mixUserList: [],
            //  票据类型
            receiptTypeList: [],
            //  特殊结算类型
            specialClearTypeList: [],
            //  道路类型
            roadTypeList: [],
            //  erp发货单
            erpInvDialog: {
                show: false,
                id: null,
            },
            //  工控消耗
            conInvoice: {
                conName: null,
                site: null,
                strengthName: null,
            },
            activeName: '',
            batchExpends: [],
            //  消耗数据
            expendDialog: {
                show: false,
                loading: false,
            },
            //  关联发货单弹窗
            linkInvDialog: {
                show: false,
                loading: false,
            },
            //  关联发货单表单
            linkInvForm: {
                erpControlId: null,
                erpInvoiceId: null,
            }
        }
    },
    // 在模板渲染成html前调用
    created() {
        var that = this;
        that.loadBaseDatas();
        that.loadTableDatas();
        that.loadCarList();
        that.loadDispatcherList();
        that.loadMixUserList();
    },
    // 在模板渲染成html后调用，通常是初始化页面完成后，再对html的dom节点进行一些需要的操作
    mounted() {
        var that = this;
    },
    methods: {
        searchDatas() {
            let that = this;
            that.tableSearch.pageIndex = 0;
            that.tableSearch.limit = 50;
            that.loadTableDatas();
        },
        //  加载各个基础数据
        loadBaseDatas() {
            let that = this;
            $.ajax({
                type: "POST",
                url: "/pro/invoice/base/datas",
                async: true,
                dataType: 'json',
                data: {},
                success: function (res) {
                    if (res.code == 0) {
                        let map = res.data;
                        that.receiptTypeList = map.receiptTypeList;
                        that.specialClearTypeList = map.specialClearTypeList;
                        that.roadTypeList = map.roadTypeList;
                    } else {
                        that.$message({
                            showClose: true,
                            message: '查询泵送设备失败：' + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '请求失败，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                    that.tableLoading = false;
                }
            })
        },
        //  加载车辆
        loadCarList() {
            let that = this;
            $.ajax({
                type: "POST",
                url: "/pro/tran/car/list",
                async: true,
                dataType: 'json',
                data: {},
                success: function (res) {
                    if (res.code == 0) {
                        that.carList = res.data;
                    } else {
                        that.$message({
                            showClose: true,
                            message: '查询车辆列表失败：' + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '请求车辆列表失败，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                }
            })
        },
        //  加载调度人员
        loadDispatcherList() {
            //  加载拌台操作员
            let that = this;
            $.ajax({
                type: "POST",
                url: "/sys_user/dispatcher/list",
                async: true,
                dataType: 'json',
                data: {},
                success: function (res) {
                    if (res.code == 0) {
                        that.dispatcherList = res.data;
                    } else {
                        that.$message({
                            showClose: true,
                            message: '查询调度员列表失败：' + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '请求调度员列表失败，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                }
            })
        },
        //  加载拌台操作员
        loadMixUserList() {
            let that = this;
            $.ajax({
                type: "POST",
                url: "/sys_user/mixer/list",
                async: true,
                dataType: 'json',
                data: {},
                success: function (res) {
                    if (res.code == 0) {
                        that.mixUserList = res.data;
                    } else {
                        that.$message({
                            showClose: true,
                            message: '查询拌台操作员列表失败：' + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '请求查询拌台操作员失败，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                }
            })
        },
        // 加载数据
        loadTableDatas: function () {
            let that = this;
            that.tableLoading = true;
            $.ajax({
                type: "POST",
                url: "/control/invoice/page",
                async: true,
                dataType: 'json',
                data: that.tableSearch,
                success: function (res) {
                    if (res.code == 0) {
                        let page = res.data;
                        that.tableDatas = page.records;
                        that.tableSearch.total = page.total;
                    } else {
                        that.$message({
                            showClose: true,
                            message: '查询发货单失败：' + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '请求失败，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                    that.tableLoading = false;
                }
            })
        },
        showAddDialog(controlInv) {
            let that = this;
            that.addInvDialog.show = true;
            //  设置参数
            let concreteStere = controlInv.concreteStere;
            let mortarStere = controlInv.mortarStere;
            let proStere = !isNull(concreteStere) ? concreteStere : mortarStere;
            that.addInvForm.render.sendStere = proStere;
            that.addInvForm.render.proStere = proStere;
            that.addInvForm.render.proTime = controlInv.proTime;
            that.addInvForm.controlInvId = controlInv.id;
        },
        //  提交表单
        addSubmit(formName) {
            let that = this;
            that.$refs[formName].validate((valid) => {
                if (valid) {
                    //  验证成功
                    that.addInvDialog.loading = true;
                    $.ajax({
                        type: "PUT",
                        url: "/control/invoice/add/" + that.addInvForm.controlInvId,
                        async: true,
                        dataType: 'json',
                        data: that.addInvForm.render,
                        success: function (res) {
                            if (res.code == 0) {
                                that.$message({
                                    showClose: true,
                                    message: '操作成功',
                                    type: 'success'
                                });
                                that.cancel();
                                that.loadTableDatas();
                            } else {
                                that.$message({
                                    showClose: true,
                                    message: '保存失败：' + res.msg,
                                    type: 'error'
                                });
                            }
                        },
                        error: function (res) {
                            that.$message({
                                showClose: true,
                                message: '请求失败，请联系管理员',
                                type: 'error'
                            });
                            return;
                        },
                        complete: function (XMLHttpRequest, status) {
                            that.addInvDialog.loading = false;
                        }
                    })
                } else {
                    that.$message({
                        showClose: true,
                        message: '验证表单失败.',
                        type: 'error'
                    });
                    return false;
                }
            });
        },
        indentChange(indentId) {
            let that = this;
            that.addInvForm.render.indentId = indentId;
        },
        //  调度人员变动
        dispatcherChange(item) {
            let that = this;
            that.addInvForm.render.dispatcher = item.id;
            that.addInvForm.render.dispatcherName = item.name;
        },
        mixUserChange(item) {
            let that = this;
            that.addInvForm.render.mixUser = item.id;
            that.addInvForm.render.mixUserName = item.name;
        },
        //  取消添加
        cancel() {
            let that = this;
            Object.keys(that.addInvForm.render).forEach(key => {
                that.addInvForm.render[key] = null;
            });
            that.linkInvForm.erpControlId = null;
            that.linkInvForm.erpInvoiceId = null;
            that.addInvDialog.show = false;
            that.linkInvDialog.show = false;
        },
        showLinkDialog(controlInv) {
            let that = this;
            that.linkInvDialog.show = true;
            //  设置参数
            that.linkInvForm.erpControlId = controlInv.id;
        },
        //  选择的发货单变动
        invoiceChange(inv) {
            let that = this;
            that.linkInvForm.erpInvoiceId = this.id;
        },
        //  关联发货单
        linkSubmit() {
            let that = this;
            //  工控机发货单
            let erpControlId = that.linkInvForm.erpControlId;
            let erpInvoiceId = that.linkInvForm.erpInvoiceId;
            //  验证数据
            if (isNull(erpControlId) || isNull(erpInvoiceId)) {
                that.$message({
                    showClose: true,
                    message: '请选择正确的发货单.',
                    type: 'error'
                });
                return;
            }
            //  验证成功
            that.linkInvDialog.loading = true;
            $.ajax({
                type: "PUT",
                url: "/control/invoice/link",
                async: true,
                dataType: 'json',
                data: that.linkInvForm,
                success: function (res) {
                    if (res.code == 0) {
                        that.$message({
                            showClose: true,
                            message: '操作成功',
                            type: 'success'
                        });
                        that.loadTableDatas();
                        that.cancel();
                    } else {
                        that.$message({
                            showClose: true,
                            message: '保存失败：' + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '请求失败，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                    that.linkInvDialog.loading = false;
                }
            })
        },
        // 修改每页数据
        handleSizeChange(val) {
            var that = this;
            that.tableSearch.limit = val;
            // 查询数据
            that.loadTableDatas();
        },
        handleCurrentChange(val) {
            // 设置页数
            var that = this;
            that.tableSearch.pageIndex = val;
            that.loadTableDatas();
        },
        //  同步工控数据
        syncControl() {
            let that = this;
            $.ajax({
                type: "POST",
                url: "/control/invoice/sync",
                async: true,
                dataType: 'json',
                data: {},
                success: function (res) {
                    if (res.code == 0) {

                    } else {
                        that.$message({
                            showClose: true,
                            message: '操作失败:' + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {

                    return;
                },
                complete: function (XMLHttpRequest, status) {
                }
            })
        },
        syncControlAll() {
            let that = this;
            $.ajax({
                type: "POST",
                url: "/control/invoice/sync/all",
                async: true,
                dataType: 'json',
                data: {
                    startTime: that.tableSearch.startTime,
                    endTime: that.tableSearch.endTime,
                },
                success: function (res) {
                    if (res.code == 0) {

                    } else {
                        that.$message({
                            showClose: true,
                            message: '操作失败:' + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {

                    return;
                },
                complete: function (XMLHttpRequest, status) {
                }
            })
        },
        //  显示发货单弹窗
        showInvoiceDialog(controlInvId) {
            let that = this;
            //  查询发货单信息
            $.ajax({
                type: "POST",
                url: "/control/invoice/erp/invoice/" + controlInvId,
                async: true,
                dataType: 'json',
                data: {},
                success: function (res) {
                    if (res.code == 0) {
                        that.erpInvDialog.id = res.data;
                        that.erpInvDialog.show = true;
                    } else {
                        that.$message({
                            showClose: true,
                            message: '查询发货单失败:' + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '请求失败.',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                }
            })
        },
        handleClick(tab, event) {
            /*let that = this;
            let index = tab.index;
            let dayValue = that.dayValues[index];
            //  销售人员名
            let saleMan = dayValue.sheetName;*/
        },
        //  加载消耗记录
        loadExpendDatas(conInv) {
            let that = this;
            if (isNull(conInv)) {
                that.$message({
                    showClose: true,
                    message: '请选择正确的发货单',
                    type: 'error'
                });
                return;
            }
            that.conInvoice = conInv;
            let controlInvId = conInv.id;
            that.expendDialog.show = true;
            that.expendDialog.loading = true;
            $.ajax({
                type: "post",
                url: "/control/invoice/expend/" + controlInvId,
                async: true,
                dataType: 'json',
                data: {},
                success: function (res) {
                    if (res.code == 0) {
                        let batchExpends = res.data;
                        console.log(batchExpends);
                        if (batchExpends == null || batchExpends.length == 0) {
                            that.$message({
                                showClose: true,
                                message: '该发货单没有消耗记录',
                            });
                            that.cancel();
                            return;
                        }
                        that.batchExpends = batchExpends;
                        that.activeName = batchExpends[0].batchNum;
                    } else {
                        that.$message({
                            showClose: true,
                            message: '查询发货单消耗数据失败：' + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '请求失败，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                    that.expendDialog.loading = false;
                }
            })
        },
        //  导出报表
        exportData() {
            let that = this;
            let startTime = that.tableSearch.startTime;
            if (startTime == null) {
                that.$message({
                    showClose: true,
                    message: '导出失败,请选择开始时间',
                    type: 'error'
                });
                return;
            }
            let endTime = that.tableSearch.endTime;
            if (endTime == null) {
                that.$message({
                    showClose: true,
                    message: '导出失败,请选择截止时间',
                    type: 'error'
                });
                return;
            }
            that.tableLoading = true;
            $.ajax({
                type: "POST", //提交方式
                url: "/control/invoice/export",//路径
                xhrFields: {responseType: "arraybuffer"},
                data: that.tableSearch,
                beforeSend: function (request) {
                    request.setRequestHeader("Authorization", "xxx");
                },
                success: function (result, textStatus, request) {
                    let responseHeader = decodeURIComponent(escape(request.getResponseHeader("Content-Disposition")));
                    let s = responseHeader.split("filename=");
                    let filename = s[1];
                    const blob = new Blob([result], {type: "application/vnd.ms-excel"});
                    if (blob.size < 1) {
                        that.$message({
                            showClose: true,
                            message: '导出失败，导出的内容为空！',
                            type: 'error'
                        });
                        return
                    }
                    if (window.navigator.msSaveOrOpenBlob) {
                        navigator.msSaveOrOpenBlob(blob, filename)
                    } else {
                        const aLink = document.createElement('a');
                        aLink.style.display = 'none';
                        aLink.href = window.URL.createObjectURL(blob);
                        aLink.download = filename;
                        document.body.appendChild(aLink);
                        aLink.click();
                        document.body.removeChild(aLink);
                    }

                },
                error: function () {

                },
                complete: function () {
                    that.tableLoading = false;
                }
            });
        },
    }
})
